package com.example.securitytest.controller;


import com.example.securitytest.domain.Item;
import com.example.securitytest.domain.ResponseResult;
import com.example.securitytest.service.ItemService;
import com.example.securitytest.utils.Limiting;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "仓库管理")
@Slf4j
@RestController
@RequestMapping("/warehouse")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Limiting(limitNum = 0.5, name = "limiting1")
    @Schema(title = "获取所有商品信息")
    @PreAuthorize("hasAnyAuthority('0','1')")
    @GetMapping
    public ResponseResult getAllItems() {

        log.info("Fetching all items from the warehouse");
        List<Item> items = itemService.getAllItems();
        log.info("Retrieved {} items", items.size());
        return new ResponseResult<>(200, "Success", items);
    }

    @Limiting(limitNum = 0.5, name = "limiting2")
    @Schema(title = "获取商品信息")
    @PreAuthorize("hasAnyAuthority('0','1')")
    @GetMapping("/{name}")
    public ResponseResult getItemByName(@PathVariable("name") String name) {
        log.info("Fetching item with name: {}", name);
        Item item = itemService.getItemByName(name);
        if (item == null) {
            log.warn("Item not found with name: {}", name);
            return new ResponseResult<>(404, "Item not found", null);
        } else {
            log.info("Item found: {}", name);
            return new ResponseResult<>(200, "Success", item);
        }
    }

    @Limiting(limitNum = 0.5, name = "limiting3")
    @Schema(title = "创建商品")
    @PreAuthorize("hasAnyAuthority('1')")
    @PostMapping
    public ResponseResult createItem(@RequestBody Item item) {
        log.info("Creating new item with name: {}", item.getName());
        Item createdItem = itemService.createItem(item);
        log.info("Item created with name: {}", createdItem.getName());
        //return ResponseEntity.status(HttpStatus.CREATED).body(createdItem);
        return new ResponseResult<>(201, "Success", createdItem);
    }

    @Limiting(limitNum = 0.5, name = "limiting4")
    @Schema(title = "更新商品")
    @PreAuthorize("hasAnyAuthority('1')")
    @PutMapping("/{name}")
    public ResponseResult updateItem(@PathVariable("name") String name, @RequestBody Item item) {
        log.info("Updating item with name: {}", name);
        Item updatedItem = itemService.updateItem(name, item);
        log.info("Item updated: {}", updatedItem.getName());
        return new ResponseResult<>(200, "Success", updatedItem);
    }

    @Limiting(limitNum = 0.5, name = "limiting5")
    @Schema(title = "删除商品")
    @PreAuthorize("hasAnyAuthority('1')")
    @DeleteMapping("/{name}")
    public ResponseResult deleteItem(@PathVariable("name") String name) {
        log.info("Deleting item with name: {}", name);
        itemService.deleteItem(name);
        Map<String, String> response = new HashMap<>();
        response.put("message", "Item deleted successfully");
        log.info("Item deleted successfully: {}", name);
        return new ResponseResult<>(200, "Success", response);
    }

    //批量上传
    @Limiting(limitNum = 0.5, name = "limiting6")
    @Schema(title = "批量上传商品")
    @PreAuthorize("hasAnyAuthority('1')")
    @PostMapping("/list")
    public ResponseResult uploadItems(@RequestBody List<Item> items) {
        log.info("Uploading multiple items, count: {}", items.size());
        List<Item> uploadedItems = itemService.uploadItems(items);
        log.info("Uploaded {} items successfully", uploadedItems.size());
        return new ResponseResult<>(200, "Success", uploadedItems);
    }

    //根据价格区间查询
    @Limiting(limitNum = 0.5, name = "limiting7")
    @Schema(title = "根据价格区间查询商品")
    @PreAuthorize("hasAnyAuthority('0','1')")
    @GetMapping("/price")
    public ResponseResult getItemsByPriceRange(@RequestParam double minPrice, @RequestParam double maxPrice) {
        log.info("Fetching items with price range {} to {}", minPrice, maxPrice);
        List<Item> items = itemService.getItemsByPriceRange(minPrice, maxPrice);
        log.info("Found {} items within the price range", items.size());
        //return items;
        return new ResponseResult<>(200, "Success", items);
    }
}


